/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.data.a2b.commons;

import cn.ac.iie.ulss.data.a2b.config.Configuration;
import java.util.HashMap;
import java.util.Map;
import org.apache.avro.Protocol;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import static cn.ac.iie.ulss.data.a2b.server.Dataa2bStartUp.protocol;
import java.io.File;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;

    public static final String SRC_NAMESERVER = "srcNameserver";
    public static final String SRC_TOPIC = "srcTopic";
    public static final String SRC_GROUP = "srcGroup";
    public static final String JSONFILE = "jsonfile";
    public static final String TABLENAME = "tablename";

    public static final String DES_NAMESERVER = "desNameserver";
    public static final String DES_TOPIC = "desTopic";
    public static final String DES_GROUP = "desGroup";
    public static final String RETRY_TIMES_WHEN_SEND_FAILED = "retryTimesWhenSendFailed";
    public static final String SEND_MSG_TIMEOUT = "sendMsgTimeout";
    public static final String COMPRESS_MSG_BODY_OVER_HOWMUCH = "compressMsgBodyOverHowmuch";
    public static final String MAX_MESSAGE_SIZE = "maxMessageSize";
    public static final String CLIENT_CALLBACK_EXECUTOR_THREADS = "clientCallbackExecutorThreads";
    public static final String BATCHSIZENUM = "batchSizeNum";

    public static final String WORKER_SIZE = "workerSize";
    public static final String CONDITION = "condition";

    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    //logger
    static Logger logger = null;

    static {
        PropertyConfigurator.configure("log4j.properties");
        logger = Logger.getLogger(RuntimeEnv.class.getName());
    }

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            return false;
        }
        conf = pConf;

        getConfAndSaveAsString(SRC_NAMESERVER, false);
        getConfAndSaveAsString(SRC_TOPIC, false);
        getConfAndSaveAsString(SRC_GROUP, false);
        getConfAndSaveAsString(JSONFILE, false);
        getConfAndSaveAsString(TABLENAME, false);

        getConfAndSaveAsString(DES_NAMESERVER, false);
        getConfAndSaveAsString(DES_TOPIC, false);
        getConfAndSaveAsString(DES_GROUP, false);
        getConfAndSaveAsInteger(RETRY_TIMES_WHEN_SEND_FAILED);
        getConfAndSaveAsInteger(COMPRESS_MSG_BODY_OVER_HOWMUCH);
        getConfAndSaveAsInteger(SEND_MSG_TIMEOUT);
        getConfAndSaveAsInteger(MAX_MESSAGE_SIZE);
        getConfAndSaveAsInteger(CLIENT_CALLBACK_EXECUTOR_THREADS);
        getConfAndSaveAsInteger(BATCHSIZENUM);

        getConfAndSaveAsInteger(WORKER_SIZE);
        getConfAndSaveAsString(CONDITION, false);

        protocol = Protocol.parse(new File(RuntimeEnv.getParamAsString(RuntimeEnv.JSONFILE)));

        return true;
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }

    public static String getParamAsString(String pParamName) {
        return (String) dynamicParams.get(pParamName);
    }

    public static Integer getParamAsInteger(String pParamName) {
        return (Integer) dynamicParams.get(pParamName);
    }

    public static Boolean getParamAsBoolean(String pParamName) {
        return (Boolean) dynamicParams.get(pParamName);
    }

    public static void getConfAndSaveAsInteger(String confName) throws Exception {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            throw new Exception("parameter " + confName + " does not exist or is not defined");
        }
        try {
            dynamicParams.put(confName, Integer.parseInt(tmp));
        } catch (Exception ex) {
            throw new Exception("parameter " + confName + " 's format is wrong for " + ex.getMessage(), ex);
        }
    }

    public static void getConfAndSaveAsString(String confName, boolean allowEmpty) throws Exception {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty() && !allowEmpty) {
            throw new Exception("parameter" + confName + "does not exist or is not defined");
        }
        dynamicParams.put(confName, tmp);
    }

    public static void getConfAndSaveAsBoolean(String confName) throws Exception {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            throw new Exception("parameter " + confName + " does not exist or is not defined");
        }
        try {
            dynamicParams.put(confName, Boolean.parseBoolean(tmp));
        } catch (Exception ex) {
            throw new Exception("parameter " + confName + " 's format is wrong for " + ex.getMessage(), ex);
        }
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }
}
